package com.longge;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-06-03 14:48
 */
public class SubsetXORSum {

    List<List<Integer>> lists = new ArrayList<>();

    public int subsetXORSum(int[] nums) {
        ArrayList<Integer> path = new ArrayList<>();
        backtrack(nums, 0, path);
        return lists.stream().map(arr -> {
            int value = 0;
            for (Integer i : arr) {
                value ^= i;
            }
            return value;
        }).mapToInt(i -> i).sum();
    }

    private void backtrack(int[] nums, int start, ArrayList<Integer> path) {
        lists.add(new ArrayList<>(path));
        for (int i = start; i < nums.length; i++) {
            path.add(nums[i]);
            backtrack(nums, i + 1, path);
            path.remove(path.size() - 1);
        }

    }
}
